#include "gamemodel.h"
#include "gamepanel.h"
#include "codec.h"
#include "datamanager.h"
#include "joinroom.h"
#include "ui_gamemodel.h"
#include <QCloseEvent>



GameModel::GameModel(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::GameModel)
{
    ui->setupUi(this);

    ui->stackedWidget->setCurrentIndex(0);

    ui->information->setVisible(false);

    setFixedSize(800, 500);

    // 取出通信类的实例对象
    Communication* comm = DataManager::getInstance()->getCommunication();
    connect(comm, &Communication::playerCount, this, [=](int count) {
        showInfo(count);
        ui->information->setVisible(true);
    });

    connect(comm, &Communication::somebodyLeave, this, &GameModel::showInfo);


    // 开始游戏
    connect(comm, &Communication::startGame, this, [=](QByteArray msg) {
        this->hide();
        GamePanel* panel = new GamePanel;
        connect(panel, &GamePanel::panelClose, this, &GameModel::show);
        panel->show();
        // 初始化游戏主界面的数据
        panel->initGamePanel(msg);
        // 断开与StartGame信号的连接
        disconnect(comm, &Communication::startGame, this, nullptr);
    });

    connect(ui->stackedWidget, &QStackedWidget::currentChanged, this, [=](int index) {
        if (index == 0) {
            ui->information->setVisible(true);
        }
    });

    connect(ui->singleModeBtn, &QPushButton::clicked, this, [=](){
        GamePanel* panel = new GamePanel;
        connect(panel, &GamePanel::panelClose, this, GameModel::show);
        panel->show();
        hide();
        DataManager::getInstance()->setGameMode(DataManager::GameMode::Single);
    });

    connect(ui->netModeBtn, &QPushButton::clicked, this, [=]() {
        ui->stackedWidget->setCurrentIndex(1);
        DataManager::getInstance()->setGameMode(DataManager::GameMode::Network);
    });

    // 自动加入某个房间
    connect(ui->autoBtn, &QPushButton::clicked, this, [=]() {
        Message msg;
        msg.userName = DataManager::getInstance()->getUserName();
        msg.reqcode = RequestCode::AutoRoom; // 自动加入房间的请求码
        DataManager::getInstance()->getCommunication()->sendMessage(&msg);
        DataManager::getInstance()->setRoomMode(DataManager::RoomMode::Auto);
    });

    // 手动创建房间
    connect(ui->manualBtn, &QPushButton::clicked, this, [=]() {
        JoinRoom room(DialogType::Manual);
        room.setWindowTitle("创建房间");
        room.exec();
    });

    // 搜索房间
    connect(ui->searchBtn, &QPushButton::clicked, this, [=]() {
        JoinRoom room(DialogType::Search);
        room.setWindowTitle("搜索房间");
        room.exec();
    });
}

GameModel::~GameModel()
{
    delete ui;
}

// 显示房间的人数信息
void GameModel::showInfo(int count)
{
    QString tip = QString("当前<%1>房间玩家人数为: %2人, 正在等待其他玩家进入, 请稍等......")
                      .arg(DataManager::getInstance()->getRoomName().data())
                      .arg(count);
    ui->information->setText(tip);
}

// 窗口关闭事件
void GameModel::closeEvent(QCloseEvent *ev)
{
    // 如果是创建房间的窗口关闭，就需要进行切换到游戏模式的选择窗口
    if (ui->stackedWidget->currentIndex() == 1) {
        ui->stackedWidget->setCurrentIndex(0);
        ev->ignore();
    } else {
        // 如果把模式选择的窗口关闭了，则表示当前玩家之间与服务器断开连接
        ev->accept();
        Message msg;
        msg.reqcode = RequestCode::Goodbye;
        msg.userName = DataManager::getInstance()->getUserName();
        msg.roomName = DataManager::getInstance()->getRoomName();
        DataManager::getInstance()->getCommunication()->sendMessage(&msg);
        DataManager::getInstance()->getCommunication()->stopLoop();

    }
}
